package com.internetplus.webcore.controller;


import cn.hutool.http.HttpResponse;
import com.internetplus.webcore.entity.Business;
import com.internetplus.webcore.entity.Product;
import com.internetplus.webcore.response.Result;
import com.internetplus.webcore.service.BusinessService;
import com.internetplus.webcore.service.PictureSave;
import com.internetplus.webcore.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@RequestMapping("/product")
@Controller
public class ProductController {

    @Autowired
    private ProductService productService;
    @Autowired
    private BusinessService businessService;
    @Autowired
    private PictureSave pictureSave;

    @RequestMapping("/get_b")
    @ResponseBody
    public Result getBusiness(@RequestParam int userId) {
        return businessService.getBusiness(userId);
    }

    @RequestMapping("/get_bAll")
    @ResponseBody
    public Result getBusinessAll(@RequestParam(defaultValue = "1") int pageNum,
                                 @RequestParam(defaultValue = "10") int pageSize) {
        return businessService.getBusinessAll(pageNum, pageSize);
    }

    @RequestMapping("/update_b")
    @ResponseBody
    public Result updateBusiness(@RequestBody Business business) {
        return businessService.updateBusiness(business);
    }

    @RequestMapping("/list_product")
    @ResponseBody
    public Result findAll(@RequestParam(defaultValue = "1") int pageNum,
                          @RequestParam(defaultValue = "10") int pageSize) {
        return productService.findAll(pageNum, pageSize);
    }

    @RequestMapping("/target_product")
    @ResponseBody
    public Result findById(@RequestParam int productid) {
        return productService.findTargetById(productid);
    }

    @RequestMapping("/target_product_userid")
    @ResponseBody
    public Result findByUserid(@RequestParam int userid) {
        return productService.findTargetByUserId(userid);
    }

    @RequestMapping("/add_product")
    @ResponseBody
    public Result addproduct(@RequestBody Product product) {
        System.out.println(product.toString());
        return productService.addProduct(product);
    }

    @RequestMapping("/delete_product")
    @ResponseBody
    public Result deleteproduct(@RequestBody List<Integer> list) {
        return productService.deleteProduct(list);
    }

    @RequestMapping("/updata_product")
    @ResponseBody
    public Result updataproduct(@RequestBody Product product) {
        return productService.saveProduct(product);
    }

    @RequestMapping("/picture_upload")
    @ResponseBody
    public Result uploadPicture(@RequestParam("img")MultipartFile img) throws IOException {

       return Result.success(pictureSave.writeimg2local(img));

    }

    @RequestMapping("/picture_load")
    @ResponseBody
    public void loadPicture(String Path, HttpServletResponse response) throws IOException {

        response.setContentType("D:\\test\\");
        String imgPath = URLDecoder.decode(Path,"utf-8");
        System.out.println(imgPath);

        File file = new File(imgPath);
        if(file.exists()){
            FileInputStream in = new FileInputStream(file);
            OutputStream os = response.getOutputStream();
            byte[] b = new  byte[1024];
            while (in.read(b)!=-1){
                os.write(b);
            }
            in.close();
            os.flush();
            os.close();
        }

    }


}
